import http from "../utils/request"
import {PageRequestType, ResponseType, PageResponseType} from "../types/Response";
import {AxiosResponse} from "axios";

const apiPrefix:string = "/type";

export interface TypeType {
    // 主键
    id?:string|null|undefined
    // 类型
    type?:string|null|undefined
    // 父节点ID
    parent_id?:string|null|undefined
    // 级别（最高三级）
    level?:number|null|undefined
    // 描述信息
    description?:string|null|undefined
    // 子级列表
    children?:TypeType[]|null
}

export interface TypePageSearchType extends PageRequestType, TypeType {

}


export async function addType(data: TypeType): Promise<AxiosResponse<ResponseType<null>>> {
    return await http.request({
        data: data,
        url: apiPrefix,
        method: 'post'
    })
}

export async function deleteTypeById(id: string): Promise<AxiosResponse<ResponseType<null>>> {
    return await http.request({
        params: {
            id
        },
        url: apiPrefix+'/id',
        method: 'delete'
    })
}

export async function deleteTypeByIds(ids: string[]): Promise<AxiosResponse<ResponseType<null>>> {
    return await http.request({
        params: {
            ids
        },
        url: apiPrefix+'/batch',
        method: 'delete'
    })
}


export async function searchTypeById(id: string): Promise<AxiosResponse<ResponseType<TypeType>>> {
    return await http.request({
        params: {
            id
        },
        url: apiPrefix+'/id',
        method: 'get'
    })
}

export async function searchType(params?: TypeType): Promise<AxiosResponse<ResponseType<TypeType[]>>> {
    return await http.request({
        params: params,
        url: apiPrefix,
        method: 'get'
    })
}
export async function searchTypeAllAsTree(): Promise<AxiosResponse<ResponseType<TypeType[]>>> {
    return await http.request({
        url: apiPrefix+'/tree',
        method: 'get'
    })
}

export async function searchTypeByPage(params: TypePageSearchType): Promise<AxiosResponse<ResponseType<PageResponseType<TypeType>>>> {
    return await http.request({
        params: params,
        url: apiPrefix+'/page',
        method: 'get'
    })
}


export async function updateTypeById(data: TypeType): Promise<AxiosResponse<ResponseType<null>>> {
    return await http.request({
        data: data,
        url: apiPrefix,
        method: 'put'
    })
}
